<?php
namespace Api\Controller;
use Think\Controller;
class CharacterController extends Controller {

    /**
     * 查询角色列表
     */
    public function chara_list() {
        $offset = intval(I('get.offset'));
        $limit = intval(I('get.limit'));
        $order = strtoupper(I('get.order'));

        $sql = sprintf('SELECT guid, account, name, level, money, totaltime '
            . 'FROM characters ORDER BY guid %s LIMIT %d OFFSET %d;', $order, $limit, $offset);

        $model = M();
        $model->db(1, "DB_CHARACTERS");
        $charaCount = $model->query('SELECT COUNT(guid) AS chara_count FROM characters;');
        $charaList = $model->query($sql);
        foreach ($charaList as &$chara) {
            $chara['totaltime'] = intval(intval($chara['totaltime']) / 60);
        }
        echo json_encode(array(
            'rows'  => $charaList,
            'total' => $charaCount[0]['chara_count']
        ));
    }
}